package com.stylefeng.guns.config;

import com.stylefeng.guns.core.intercept.BattleWebSocketInterceptor;
import com.stylefeng.guns.modular.flagglory.websocket.BattleWebSocketPushHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * Created by likun on 2018/2/8.
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {


    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(BattleWebSocketPushHandler(), "/front/webSocketServer").addInterceptors(new BattleWebSocketInterceptor()).setAllowedOrigins("*");
        webSocketHandlerRegistry.addHandler(BattleWebSocketPushHandler(), "/sockjs/webSocketServer")
                .addInterceptors(new BattleWebSocketInterceptor()).withSockJS();
    }

    @Bean
    public WebSocketHandler BattleWebSocketPushHandler() {
        return new BattleWebSocketPushHandler();
    }
}
